package es.unileon.is.finalproyect.texts;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import es.unileon.is.finalproyect.dbconnection.ApplicationConnection;
import es.unileon.is.finalproyect.users.User;

public class Question {
	private String _correctAnswer;
	private String _incorrectAnswer1;
	private String _incorrectAnswer2;
	private String _incorrectAnswer3;
	private String _question;
	private int _questionNumber;
	private int _textID;
	
	public String[] getAnswers(){
		String[] answers = new String[4];
		answers[0]=new String(_correctAnswer);
		answers[1]=new String(_incorrectAnswer1);
		answers[2]=new String(_incorrectAnswer2);
		answers[3]=new String(_incorrectAnswer3);
		return answers;
	}
	
	public String getQuestion(){
		return new String(_question);
	}
	
	public int getQuestionNumber(){
		return _questionNumber;
	}
	
	public int getTextId(){
		return _textID;
	}
	
	public void setAnswers(User admin, String[] answ){
		if(!admin.isAdministrator()){
			throw new RuntimeException("You do not have enough privileges to perform this operation");
		}
		if(answ.length != 4){
			throw new RuntimeException("4 answers are necessary and the first one must be the right one");
		}
		try {
			PreparedStatement ps = ApplicationConnection.getConnection().prepareStatement("UPDATE QUESTIONS SET correctAnswer = ?, incorrectAnswer1 = ?, incorrectAnswer2 = ?, incorrectAnswer3 = ? WHERE textNumber = ? AND questionNumber = ?");
			ps.setString(1, answ[0]);
			ps.setString(2, answ[1]);
			ps.setString(3, answ[2]);
			ps.setString(4, answ[3]);
			ps.setInt(5, _textID);
			ps.setInt(6, _questionNumber);
			ps.execute();
			_correctAnswer=answ[0];
			_incorrectAnswer1=answ[1];
			_incorrectAnswer2=answ[2];
			_incorrectAnswer3=answ[3];
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	
	public void setQuestion(User admin, String q){
		if(!admin.isAdministrator()){
			throw new RuntimeException("You do not have enough privileges to perform this operation");
		}
		try {
			PreparedStatement ps = ApplicationConnection.getConnection().prepareStatement("UPDATE QUESTIONS SET question = ? WHERE textNumber = ? AND questionNumber = ?");
			ps.setString(1, q);
			ps.setInt(2, _textID);
			ps.setInt(3, _questionNumber);
			ps.execute();
			_question=q;
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage());
		}
	}	
	
	public Question(User admin, int textID, String question, String answers[]){
		if(!admin.isAdministrator()){
			throw new RuntimeException("You do not have enough privileges to perform this operation");
		}
		_textID = textID;
		_question = question;
		if(answers.length != 4){
			throw new RuntimeException("4 answers are necessary and the first one must be the right one");
		}
		_correctAnswer = answers[0];
		_incorrectAnswer1 = answers[1];
		_incorrectAnswer2 = answers[2];
		_incorrectAnswer3 = answers[3];
		try{
			PreparedStatement ps = ApplicationConnection.getConnection().prepareStatement("INSERT INTO QUESTIONS (textNumber, question, correctAnswer, incorrectAnswer1, incorrectAnswer2, incorrectAnswer3) VALUES (?,?,?,?,?,?)");
			ps.setInt(1, _textID);
			ps.setString(2, _question);
			ps.setString(3, _correctAnswer);
			ps.setString(4,_incorrectAnswer1);
			ps.setString(5, _incorrectAnswer2);
			ps.setString(6, _incorrectAnswer3);
			ps.execute();
			
			ps = ApplicationConnection.getConnection().prepareStatement("SELECT max(questionNumber) FROM QUESTIONS WHERE textNumber = ?");
			ps.setInt(1, _textID);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				_questionNumber = rs.getInt(1);
			} else {
				throw new RuntimeException("Question has not been properly stored");
			}
		} catch (SQLException e){
			throw new RuntimeException(e.getMessage());
		}
			
	}
	
	private Question(int textID, int questionNumber){
		ResultSet rs;
		_textID = textID;
		_questionNumber = questionNumber;
		try {
			rs = ApplicationConnection.getConnection().prepareStatement("SELECT question, correctAnswer, incorrectAnswer1, incorrectAnswer2, incorrectAnswer3 FROM QUESTIONS WHERE textNumber = " + _textID + " AND questionNumber = "+_questionNumber).executeQuery();
			//EXECUTEQUERY SI TENEMOS RESULTSET, SINO EXECUTE.
			if(rs.next()){
				_question=rs.getString(1);
				_correctAnswer=rs.getString(2);
				_incorrectAnswer1=rs.getString(3);
				_incorrectAnswer2=rs.getString(4);
				_incorrectAnswer3=rs.getString(5);
			} else {
				throw new RuntimeException("That question does not exist");
			}
		}catch(SQLException e){
			throw new RuntimeException(e.getMessage());
		}
	}
	
	public static Question requestQuestion(int textID, int questionNumber){
		return new Question(textID,questionNumber);
	}
	
	
}
